<template>
    <div class="login-container">
        <!-- iview的表单验证 -->
        <b-container class="bv-example-row">
            <b-row class="justify-content-md-center">
                <b-col sm="12" md="6" lg="4">
                    <a-card title="注册">
                        <Form ref="formInline" :model="formInline" :rules="ruleInline" >
                            <FormItem prop="user">
                                <Input type="text" v-model="formInline.user" placeholder="user">
                                    <Icon type="ios-call-outline" slot="prepend"></Icon>
                                </Input>
                            </FormItem>
                            <FormItem prop="phone">
                                <Input type="text" v-model="formInline.phone" placeholder="phone">
                                    <Icon type="ios-call-outline" slot="prepend"></Icon>
                                </Input>
                            </FormItem>
                            <FormItem prop="password">
                                <Input type="password" v-model="formInline.password" placeholder="Password">
                                    <Icon type="ios-lock-outline" slot="prepend"></Icon>
                                </Input>
                            </FormItem>
                            <FormItem>
                                <Button type="success" @click="handleSubmit('formInline')" long>注册</Button>
                            </FormItem>
                        </Form>
                        <div class="extra">
                            <p>
                                <span>已经拥有账户?</span><router-link to="/login">登录</router-link>
                            </p>
                        </div>
                    </a-card>
                </b-col>
            </b-row>
        </b-container>
  </div>
</template>

<script>
export default {
    data () {
        // 验证注册前用户名是否重复
        const validateUser = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请填写用户名'));
            }else {
                this.$http.get("user/registerUsername/" + value).then(result => {
                    if(result.body == true){
                        callback(new Error('此用户名已存在，请重新填写'));
                    }
                    callback();
                })
            }
        };
        return {
            formInline: {
                user:'',
                phone: '',
                password: ''
            },
            ruleInline: {
                user: [
                    { validator: validateUser, trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请填写电话', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请填写密码', trigger: 'blur' },
                    { type: 'string', min: 6, message: '请注意密码不少于6位', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        handleSubmit(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    // console.log(this.formInline.password);
                    this.$http.post(
                        "user/save",
                        JSON.stringify({
                            "uName":this.formInline.user,
                            "uAccount":this.formInline.phone,
                            "uPass":this.formInline.password
                        })).then(result => {
                            this.$Message.success(result.body.msg);
                            // 使用JS的形式，进行路由导航
                            this.$router.push("/login");
                        })
                } else {
                    this.$Message.error('Fail!');
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.login-container{
    margin-top:2rem;
    .extra{
        text-align: center;
    }
}
</style>